<?php
/**
 * Created by PhpStorm.
 * User: yzg
 * Date: 2015/9/15
 * Time: 15:00
 */

/**
 * Class Weixin_Windex_module
 */
class Weixin_Windex_module extends CI_Module
{
    public $weixin_model;
    public $Orderinfo_model;
    public $setting;
    public $sid;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->load->model('weixin.weixin_model');
        $this->load->model('expert.Expert_model');
        $this->load->model('hotel.Hotel_model');
        $this->setting = $this->weixin_model->db_get_setting();
        define('TOKEN', @$this->setting['token']);
        define('APPID', @$this->setting['appid']);
        define('APPSECRET', @$this->setting['appsecret']);
        $this->adminid = $this->session->userdata('admin_user_id');
        if (!$this->adminid) {
            redirect(site_url('admin/index/login'));
        }
        $this->load->model('order.Orderinfo_model');

        $this->load->model('member.User_model');
        $this->load->model('weixin.shop_model');
   
    }


    public function index()
    {
        $this->data['title'] = '概览';
        $this->data['view'] = 'tj_all';


        $this->data['enddate'] = date('Y-m-d',strtotime("today"));
        $this->data['startdate'] = date('Y-m-d',strtotime("-8 days"));

        //线路数量

        $this->data['total_shops'] = $this->shop_model->total_shops();
        $this->data['total_wmp'] = $this->Expert_model->get_expert_count(array('sid'=>$GLOBALS['sid']));

        //订单数量
        $this->data['order_count'] = $this->Orderinfo_model->get_order_count(array('sid'=>$GLOBALS['sid'],'order_type'=>1));

        $this->loadview($this->data, 'xenon/common/index');
    }

    public function index_plus()
    {
        $this->data['title'] = '概览';
        $this->data['view'] = 'tj_all';


        $this->data['enddate'] = date('Y-m-d',strtotime("today"));
        $this->data['startdate'] = date('Y-m-d',strtotime("-8 days"));

        //线路数量

        $this->data['total_shops'] = $this->shop_model->total_shops();
        $this->data['total_wmp'] = $this->Expert_model->get_expert_count(array('sid'=>$GLOBALS['sid']));

        //房间数量
        $this->data['room_count'] = $this->Hotel_model->get_number_count(array('sid'=>$GLOBALS['sid']));

        //菜品数量

        $this->data['product_count'] = $this->Hotel_model->get_extend_product_count(array('sid'=>$GLOBALS['sid']));

        //订单数量
        $this->data['order_count'] = $this->Orderinfo_model->get_order_count(array('sid'=>$GLOBALS['sid'],'order_type'=>1));

        $this->loadview($this->data, 'xenon/common/index_plus');
    }



    public function tj_weimingpian()
    {
        $this->data['title'] = '微名片统计';
        $this->data['view'] = 'tj_weimingpian';

        $uids = array();
        $uid = intval($this->input->get_post('uid'));

        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');
        if(empty($enddate))$enddate = date('Y-m-d',strtotime("today"));
        if(empty($startdate))$startdate= date('Y-m-d',strtotime("-1 month"));


        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
        $condition['uid'] = $uid;
        $this->data['condition'] = $condition;
        $starttime=strtotime($startdate);
        $endtime=strtotime($enddate);
        $end_day=round(($endtime-$starttime)/86400);
        $add = date('j',$starttime);

        $date_array = array();
        for($day=$add; $day<=$end_day+$add; $day++){
            $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$starttime),$day,date('Y',$starttime)));//
            array_push($date_array,$temp_date);
        }

        $this->data['views'] = $this->Expert_model->fetch_count_wmp($date_array);
        $this->data['share'] = $this->Expert_model->fetch_share_count_wmp($date_array);

        $this->data['date_array'] = $date_array;

        if($_GET['ifr']){
            $this->loadview($this->data, 'xenon/tj_weimingpian');
        }else {
            $this->loadview($this->data, 'xenon/common/index');
        }
    }

    public function tj_user()
    {
        $this->data['title'] = '用户统计';
        $this->data['view'] = 'tj_user';

        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');
        $type = $this->input->get_post('type');//1按天2按月3按年


        if(empty($enddate))$enddate = date('Y-m-d',strtotime("today"));
        if(empty($startdate))$startdate= date('Y-m-d',strtotime("-30 day"));
        $type = $type?$type:1;


        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
        $this->data['condition'] = $condition;
        $this->data['type_name'] = '新注册会员';

        $start_time = strtotime($startdate);
        $end_time   = strtotime($enddate);
        $end_day = round(($end_time-$start_time)/86400);
        $add = date('j',$start_time);
        $date_array = array();
        $formate    = $format ='';
        if($type==3){
            $formate="'%Y'";
            $format = 'Y';
            $where = "reg_date>='".date('Y',$start_time)."' AND reg_date<='".date('Y',$end_time)."-12-31 23:59:59'";
        }elseif($type==2){
            $formate="'%Y-%m'";
            $format = 'Y-m';
            $where = "reg_date>='".date('Y-m',$start_time)."' AND reg_date<='".date('Y-m',$end_time)."-31 23:59:59'";
        }else{
            for($day=$add; $day<=$end_day+$add; $day++){
                $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$start_time),$day,date('Y',$start_time)));//
                array_push($date_array,$temp_date);
            }

            $formate="'%Y-%m-%d'";
            $format = 'Y-m-d';
            $where = "reg_date>='".date('Y-m-d',$start_time)."' AND reg_date<='".date('Y-m-d',$end_time)." 23:59:59'";
        }
        $list = $this->User_model->user_chart($formate,$where);

        foreach($date_array as $key=>$val){
            if(in_array($val,array_keys($list))){
                $data_array[$key] = $list[$val];
            }else{
                $data_array[$key] = 0;
            }
        }

        $this->data['date_array'] = $date_array;
        $this->data['data_array'] = $data_array;
        if($_GET['ifr']){
            $this->loadview($this->data, 'xenon/tj_user');
        }else{
            $this->loadview($this->data, 'xenon/common/index');
        }
    }

    public function tj_weidian()
    {
        $this->data['title'] = '微店统计';
        $this->data['view'] = 'tj_weidian';

        $uids = array();
        $uid = intval($this->input->get_post('uid'));

        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');
        if(empty($enddate))$enddate = date('Y-m-d',strtotime("today"));
        if(empty($startdate))$startdate= date('Y-m-d',strtotime("-1 month"));


        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
        $condition['uid'] = $uid;
        $this->data['condition'] = $condition;
        $starttime=strtotime($startdate);
        $endtime=strtotime($enddate);
        $end_day=round(($endtime-$starttime)/86400);
        $add = date('j',$starttime);

        $date_array = array();
        for($day=$add; $day<=$end_day+$add; $day++){
            $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$starttime),$day,date('Y',$starttime)));//
            array_push($date_array,$temp_date);
        }

        $ret = $this->shop_model->fetch_count($uid, $date_array);

        foreach ($ret as $k => $v) {
            $this->data['date_array'][] = $k;
            $ordernum = $callnum = $pv = $share = 0;
            foreach ($v as $vv) {
                $pv += $vv['pv'];
                $share += $vv['share'];
                $callnum += $vv['callnum'];
                $ordernum += $vv['ordernum'];
                $uids[$vv['pid']] = $vv['pid'];
            }

            $this->data['pv'][] = $pv;
            $this->data['share'][] = $share;
            $this->data['callnum'][] = $callnum;
            $this->data['ordernum'][] = $ordernum;

        }

        $list = $this->shop_model->get_shop_list(' shop_status=1 ', 200);
        foreach ($list as $v) {
            $this->data['list'][$v['user_id']] = $v['shop_name'];
        }
        if($_GET['ifr']){
            $this->loadview($this->data, 'xenon/tj_weidian');
        }else {
            $this->loadview($this->data, 'xenon/common/index');
        }
    }

    public function tj_order()
    {
        $this->data['title'] = '订单统计';
        $this->data['view'] = 'tj_order';

/*
        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');*/
       $enddate = date('Y-m-d',strtotime("+1 day"));
       $startdate= date('Y-m-d',strtotime("-1 month"));

        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
        $this->data['condition'] = $condition;
        $starttime=strtotime($startdate);
        $endtime=strtotime($enddate);
        $end_day=round(($endtime-$starttime)/86400);
        $add = date('j',$starttime);

        $date_array = array();
        for($day=$add; $day<=$end_day+$add; $day++){
            $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$starttime),$day,date('Y',$starttime)));//
            array_push($date_array,$temp_date);
        }

        $cattype = array( 1);
        $kk = count($date_array) -1;

        $data_array1 = $this->Orderinfo_model->fetch_count($date_array, array(1), $cattype);

        $data_array1[$kk] = $this->Orderinfo_model->fetch_count_by_date($date_array[$kk], '', array(1), $cattype);

        $data_array2 = $this->Orderinfo_model->fetch_count($date_array, array(2), $cattype);
     
        $data_array2[$kk] = $this->Orderinfo_model->fetch_count_by_date($date_array[$kk], '', array(2), $cattype);


        $this->data['kk'] = $kk;
        $this->data['date_array'] = $date_array;
        $this->data['data_array1'] = $data_array1;
        $this->data['data_array2'] = $data_array2;
        if($_GET['ifr']){
            $this->loadview($this->data, 'xenon/tj_order');
        }else{
            $this->loadview($this->data, 'xenon/common/index');
        }
    }

    /**
     * 线路统计
     */
    public function tj_line()
    {
        $this->data['title'] = '订单统计';
        $this->data['view'] = 'line_chart';

        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');
        $type = $this->input->get_post('type');//views浏览量shares分享量adds添加量
        $type_array = array('views'=>'线路浏览量','shares'=>'线路分享量','adds'=>'线路添加量');


        if(empty($enddate))$enddate = date('Y-m-d',strtotime("today"));
        if(empty($startdate))$startdate= date('Y-m-d',strtotime("-1 month"));
        if(empty($type))$type = 'views';


        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
        $condition['type']      = $type;
        $this->data['condition'] = $condition;
        $this->data['type_name'] = $type_array[$type];
        $starttime=strtotime($startdate);
        $endtime=strtotime($enddate);
        $end_day=round(($endtime-$starttime)/86400);
        $add = date('j',$starttime);
        $date_array = array();
        for($day=$add; $day<=$end_day+$add; $day++){
            //echo $day;
            $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$starttime),$day,date('Y',$starttime)));//
            array_push($date_array,$temp_date);
        }


        $this->data['date_array'] = $date_array;
        if($_GET['ifr']){
            $this->loadview($this->data, 'xenon/line_chart');
        }else {
            $this->loadview($this->data, 'xenon/common/index');
        }
    }
}
